//
//  HMGoldSentenceViewCtr.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/5.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMGoldSentenceViewCtr.h"
#import "ZLSwipeableView.h"
#import "HMGoldSentence.h"
#import "HMHomeGoldSentenceVM.h"
#import "HMGlodSTView.h"
#import "HMGoldSTBootomBar.h"
#import "HMDetailsTableViewController.h"
@interface HMGoldSentenceViewCtr ()<ZLSwipeableViewDataSource,ZLSwipeableViewDelegate>
@property(nonatomic,strong)HMHomeGoldSentenceVM  *goldVM;
//记录当前播放的是某一条
@property(nonatomic,assign)NSInteger currentIndex;
@property(nonatomic,strong)ZLSwipeableView *swipeView;
@property(nonatomic,strong)HMGoldSTBootomBar  *bottomToolBar;
@end

@implementation HMGoldSentenceViewCtr
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setBackGroudViewColorWithImg:[UIImage imageNamed:@"whiteBg"]];
    [self configurUI];
    [self loadData];
}
-(void)configurUI
{
    self.navigationItem.title = @"看金句";
    self.currentIndex = 0;
    [self creatSwipeableView];
    [self.view addSubview:self.bottomToolBar];
    [self.bottomToolBar mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.right.equalTo(self.view);
        make.height.offset(49);
        make.bottom.equalTo(self.view).mas_offset(-10);
    }];
#pragma mark --查询来源跳转
    __weak typeof(self)weakSelf = self;
    self.bottomToolBar.pushTosourceFromDetial = ^
    {
        HMGoldSentence *sentence = weakSelf.goldVM.goldSMList[weakSelf .currentIndex];
        HMDetailsTableViewController* detail = [[HMDetailsTableViewController alloc]init];
        detail.tid = sentence.ebook_id;
        [weakSelf .navigationController pushViewController:detail animated:YES];
    };
}
-(void)creatSwipeableView
{
    ZLSwipeableView *swipeableView = [[ZLSwipeableView alloc] initWithFrame:CGRectZero];
    self.swipeView = swipeableView;
    [self.view addSubview:self.swipeView];
    // Required Data Source
    self.swipeView.dataSource = self;
    // Optional Delegate
    self.swipeView.delegate = self;
    self.swipeView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *metrics = @{};
    //添加约束的方法
    [self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"|-50-[swipeableView]-50-|"
                               options:0
                               metrics:metrics
                               views:NSDictionaryOfVariableBindings(
                                                                    swipeableView)]];
    
    [self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"V:|-120-[swipeableView]-100-|"
                               options:0
                               metrics:metrics
                               views:NSDictionaryOfVariableBindings(
                                                                    swipeableView)]];
}
#pragma mark --dataSource
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView
{
    if (self.goldVM.goldSMList.count == 0)
    {
        return nil;
    }
    if (self.currentIndex >= self.goldVM.goldSMList.count)
    {
        self.currentIndex = 0;
    }
    HMGlodSTView *view = [[HMGlodSTView alloc] initWithFrame:swipeableView.bounds];
    view.stence = self.goldVM.goldSMList[self.currentIndex];
    self.bottomToolBar.stence = self.goldVM.goldSMList[self.currentIndex];
    self.currentIndex ++;
    return view;
}
- (UIView *)previousViewForSwipeableView:(ZLSwipeableView *)swipeableView
{
    HMGlodSTView *view = [[HMGlodSTView alloc] initWithFrame:swipeableView.bounds];
    view.stence = self.goldVM.goldSMList[self.currentIndex];
    return view;
}
//设置背景图片的方法；
-(void)setBackGroudViewColorWithImg:(UIImage *)img
{
  self.view.layer.contents = (__bridge id _Nullable)(img.CGImage);
}
//请求数据的方法
-(void)loadData
{
    __weak typeof(self)weakSelf = self;
  [self.goldVM loadGoldSentenceData:^(NSError *error)
    {
        weakSelf.bottomToolBar.stence = weakSelf.goldVM.goldSMList[self.currentIndex];
        [weakSelf.swipeView loadViewsIfNeeded];
  }];
}
- (HMHomeGoldSentenceVM *)goldVM
{
    if (!_goldVM)
    {
        _goldVM = [[HMHomeGoldSentenceVM alloc]init];
    }
    return _goldVM;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
     [self.swipeView loadViewsIfNeeded];
}
-(HMGoldSTBootomBar *)bottomToolBar
{
    if (!_bottomToolBar)
    {
        _bottomToolBar = [HMGoldSTBootomBar loadNibView];
    }
    return _bottomToolBar;
}

@end
